package com.hw.graphql.resolver;

import com.hw.graphql.mapper.UserMapper;
import com.hw.graphql.model.AddUserInput;
import com.hw.graphql.model.Result;
import com.hw.graphql.model.User;
import graphql.kickstart.tools.GraphQLMutationResolver;
import graphql.kickstart.tools.GraphQLQueryResolver;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.UUID;

/**
 * @program: spring-graphql
 * @description: 更新接口
 * @author: hewei
 * @date: 2021-11-23 11:28
 **/
@Slf4j
@Component
    public class UserUpdateResolver implements /*GraphQLQueryResolver,*/GraphQLMutationResolver {

    private static DateTimeFormatter dtf = DateTimeFormatter.ofPattern("YYYY-MM-dd HH:mm:ss");
    @Autowired
    private UserMapper userMapper;

    public Result addUser(String mail, String nickname, String password, String description) {
        log.info("Mutation Resolver ==> addUser");
        log.info("params: mail:{}, nickname:{}, password:{}, description:{}",
                mail, nickname, password, description);
        User user=User.builder().build();
        user.setMail(mail);
        user.setNickname(nickname);
        user.setPassword(password);
        user.setDescription(description);
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        userMapper.insert(user);
        return new Result(200,"成功");
    }

    public Result deleteUser(String id) {
        if(StringUtils.isAnyBlank(id)){
            return new Result(-101, "参数缺失");
        }
        log.info("Mutation Resolver ==> deleteUser");
        log.info("params: id:{}", id);
        userMapper.deleteById(id);
        return new Result(200,"成功");
    }

    public User updateUser(String id, String mail, String nickname, String description) {
        log.info("Mutation Resolver ==> updateUser");
        log.info("params: id:{}, mail:{}, nickname:{}, description:{}",
                id, mail, nickname, description);
        User user=new User(id,nickname,mail,null,description,new Date(),new Date());
        userMapper.updateById(user);
        return userMapper.selectById(id);
    }

    public User addUserByInput(AddUserInput addUserInput){
        log.info("Mutation Resolver ==> addUserByInput");
        log.info("params: {}", addUserInput);
        String id=UUID.randomUUID().toString();
        User user=User.builder()
                .id(id)
                .nickname(addUserInput.getNickname())
                .mail(addUserInput.getMail())
                .description(addUserInput.getDescription())
                .password(addUserInput.getPassword())
                .createTime(addUserInput.getCreateTime())
                .updateTime(addUserInput.getUpdateTime())
                .build();
        userMapper.insert(user);
        return userMapper.selectById(id);
    }
}
